#!/usr/bin/env python
"""
NSCA (Nagios Remote Plugin Executor) is a protocol that pushes 
the results of Nagios Plugins, which were collected by a local
Nagios server, to a central Nagios server. There the results are
listed as 'passive check results'. 
This script is used to feed the  NSCA protocol at the local Nagios 
with the results of the Nagios Plugins. 

In order to activate NSA support a the local Nagios server you need:

0. have the nsca client installed (pre-requisite, not described here.)
1. (in $NAGIOS_LOCATION/etc/send_nsca.cfg). Chose same encrypt_method 
    as on central Nagios server.

        ...
        encryption_method=1

2. edit this file (e.g. located at $NAGIOS_LOCATION/libexec/smscg_passive_check.py) 
    so it fits your settings (Speficically the NSCA_CFG & NSCA_BIN locations)
3. in your $NAGIOS_LOCATION/etc/nagios.cfg file set/change:

        obsess_over_services = 1
        obsess_over_hosts= 0
        ocsp_commad = smscg_passive_check

4. in your $NAGIOS_LOCAION/etc/*/commands.cfg add following service 
   definition (adjust the command_line location accordingly):

    define command{
        command_name    smscg_passive_check
        command_line    /usr/local/nagios/libexec/smscg_passive_check.py \
            $HOSTNAME$ '$SERVICEDESC$' $SERVICESTATE$ '$SERVICEOUTPUT$' '$SERVICEPERFDATA$'
    }
    
    (Notice, the 'command_line /usr/...'  is to be entered in one single line)

Last change:
12.04.12 - added DPM checks to be forwareded to central server (PF),
        - refactoring to use subprocess.

"""
__author__ = "Placi Flury placi.flury@switch.ch"
__date__ = "11.03.2010"
__version__ = "0.2.1"

import sys
from subprocess import Popen, PIPE


###### PLEASE ADAPT CONFIG HERE:

NAGIOS_SERVER = "nagios.smscg.ch"  # central server name (where to send passive check results)
NSCA_CFG = "/usr/local/nagios/etc/send_nsca.cfg"   # configuration of send_nsca command
NSCA_BIN = "/usr/local/nagios/bin/send_nsca"         # path to send_nsca command


# Names of services to export to NAGIOS_SERVER (you normally don't want to export everything)
SERVICES = ["ARC Version",
    "SGAS LUTS",
    "SGAS Aggregator",
    "Trustanchors",
    "Gridsecurity",
    "Gridftp",
    "gridftpd",
    "Gridftpd",
    "Grid-infosys",
    "Grid-manager",
    "NTP", "HTTP",
    "VOMS Core", 
    "MyProxy", 
    "GIIS",
    "VASH Core",
    "Supported VOs",
    "Grid Pool Accounts", 
    "Controldir", 
    "CPU Load", 
    "Root Partition",
    "DPM", 
    "DPM Gridftp",
    "DPM Infosys",
    "DPM Perf",
    "Swap Usage", 
    "Hostcert",
    "Total Processes"]

##### END OF CONFIGURATION              
HOST = sys.argv[1]
SVC_DESCR = sys.argv[2]
STATE = sys.argv[3]
PLUGIN_OUTPUT = sys.argv[4]

if len(sys.argv) == 6:
    PLUGIN_PERFDATA = sys.argv[5]
else:
    PLUGIN_PERDATA = None 

if SVC_DESCR not in SERVICES:
    sys.exit(0)

CODES = {'OK': 0, 'WARNING': 1, 'CRITICAL': 2, 'UNKNOWN': -1}

try:
    return_code = CODES[STATE]
except Exception:    
    return_code = CODES['UNKNOWN']


if PLUGIN_PERFDATA:
    nsca_msg = ("'%s;%s;%s;%s|%s\n'" % (HOST, SVC_DESCR,
        return_code, PLUGIN_OUTPUT, PLUGIN_PERFDATA))
else:
    nsca_msg = ("'%s;%s;%s;%s\n'" % (HOST, SVC_DESCR, 
        return_code, PLUGIN_OUTPUT))

echo = Popen(['echo', '-e', nsca_msg], stdout=PIPE) # beware whitespaces in args not stripped by Popen 

nsca = Popen([NSCA_BIN,
            '-H', NAGIOS_SERVER,
            '-c', NSCA_CFG,
            '-d', ';'], stdin=echo.stdout, stdout=PIPE)

sys.exit(nsca.returncode)

